package com.huawei.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.dds.v3.DdsClient;
import com.huaweicloud.sdk.dds.v3.model.ListInstancesRequest;
import com.huaweicloud.sdk.dds.v3.model.ListInstancesResponse;
import com.huaweicloud.sdk.dds.v3.model.ResizeInstanceVolumeOption;
import com.huaweicloud.sdk.dds.v3.model.ResizeInstanceVolumeRequest;
import com.huaweicloud.sdk.dds.v3.model.ResizeInstanceVolumeRequestBody;
import com.huaweicloud.sdk.dds.v3.model.ResizeInstanceVolumeResponse;
import com.huaweicloud.sdk.dds.v3.model.ShowJobDetailRequest;
import com.huaweicloud.sdk.dds.v3.model.ShowJobDetailResponse;
import com.huaweicloud.sdk.dds.v3.region.DdsRegion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ReplicaSetEnlargeVolumeDemo {

    private static final Logger logger = LoggerFactory.getLogger(ReplicaSetEnlargeVolumeDemo.class.getName());

    /**
     * args[0] = "<YOUR AK>"
     * args[1] = "<YOUR SK>"
     * args[2] = "<YOUR INSTANCE_ID>"
     * args[3] = "<YOUR REGION_ID>"
     *
     * @param args
     */
    public static void main(String[] args) {
        if (args.length != 4) {
            logger.info("Illegal Arguments");
        }

        String ak = args[0];
        String sk = args[1];
        String instanceId = args[2];
        String regionId = args[3];

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);

        DdsClient client = DdsClient.newBuilder()
                .withCredential(auth)
                .withRegion(DdsRegion.valueOf(regionId))
                .build();

        // 查看副本集实例存储空间
        showDdsReplicaSetDetail(client, instanceId);

        // 扩容副本集实例存储空间
        String jobId = enlargeDdsReplicaSetVolume(client, instanceId);

        // 等待实例扩容成功,根据返回的jobId，查询进度，当返回的任务status为“Completed” 时表示扩容成功
        waitEnlargeSuccess(client, jobId);

        // 查看副本集实例存储空间
        showDdsReplicaSetDetail(client, instanceId);
    }

    private static void showDdsReplicaSetDetail(DdsClient client, String instanceId) {
        ListInstancesRequest request = new ListInstancesRequest();
        request.withId(instanceId);
        try {
            ListInstancesResponse response = client.listInstances(request);
            logger.info(response.toString());
        } catch (ConnectionException e) {
            logger.error("ConnectionException", e);
        } catch (RequestTimeoutException e) {
            logger.error("RequestTimeoutException ", e);
        } catch (ServiceResponseException e) {
            logger.error("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.getHttpStatusCode(), e.getErrorCode(), e.getErrorMsg());
        }
    }

    private static String enlargeDdsReplicaSetVolume(DdsClient client, String instanceId) {
        ResizeInstanceVolumeRequest request = new ResizeInstanceVolumeRequest();
        ResizeInstanceVolumeRequestBody body = new ResizeInstanceVolumeRequestBody();
        ResizeInstanceVolumeOption option = new ResizeInstanceVolumeOption();

        // 需要大于实例当前磁盘大小
        String targetVolumeSize = "50";
        option.setSize(targetVolumeSize);
        body.setVolume(option);
        request.withBody(body);
        request.setInstanceId(instanceId);

        ResizeInstanceVolumeResponse response = new ResizeInstanceVolumeResponse();
        try {
            response = client.resizeInstanceVolume(request);
            logger.info(response.toString());
        } catch (ConnectionException e) {
            logger.error("ConnectionException", e);
        } catch (RequestTimeoutException e) {
            logger.error("RequestTimeoutException ", e);
        } catch (ServiceResponseException e) {
            logger.error("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.getHttpStatusCode(), e.getErrorCode(), e.getErrorMsg());
        }
        return response.getJobId();
    }

    private static void waitEnlargeSuccess(DdsClient client, String jobId) {
        ShowJobDetailRequest request = new ShowJobDetailRequest();
        request.withId(jobId);
        try {
            ShowJobDetailResponse response = client.showJobDetail(request);
            logger.info(response.getJob().getStatus());
        } catch (ConnectionException e) {
            logger.error("ConnectionException", e);
        } catch (RequestTimeoutException e) {
            logger.error("RequestTimeoutException ", e);
        } catch (ServiceResponseException e) {
            logger.error("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.getHttpStatusCode(), e.getErrorCode(), e.getErrorMsg());
        }
    }
}
